<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" lay-filter="search-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">审核状态</label>
                            <div class="layui-input-inline">
                                <select name="state" lay-verify="" lay-search="">
                                    <option value=""> -所有状态- </option>
                                    <option value="302">待采销审核</option>
                                    <option value="304">待产品审核</option>
                                    <option value="306">审核通过</option>
                                    <option value="303">采销驳回</option>
                                    <option value="305">产品驳回</option>
                                    <option value="309">待经理审核</option>
                                    <option value="310">经理驳回</option>
                                    <option value="311">待总监审核</option>
                                    <option value="312">总监驳回</option>
                                    <option value="313">待质量管理部审核</option>
                                    <option value="320">质量管理部驳</option>
                                    <option value="321">待质量负责人审核</option>
                                    <option value="322">质量负责人驳回</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">是否驳回</label>
                            <div class="layui-input-inline">
                                <select name="reject" lay-verify="" lay-search="">
                                    <option value=""> -所有状态- </option>
                                    <option value="true">是</option>
                                    <option value="false">否</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">SKU名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="name" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <table class="layui-hide" id="appliesTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="productAppliesTableBar">
            <!-- 这里同样支持 laytpl 语法，如： -->
            <a class="layui-btn layui-btn-xs" lay-event="reset">重新上架</a>
        </script>


    </div>
</div>

<script>
    layui.use(['form', 'table','miniPage','element', 'util'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage,
            util = layui.util;

        form.render();


        var map = {
            302: "待采销审核",
            304: "待产品审核",
            306: "<span style='color: green'>审核通过</span>",
            303: "采销驳回",
            305: "产品驳回",
            309: "待经理审核",
            310: "经理驳回",
            311: "待总监审核",
            312: "总监驳回",
            313: "待质量管理部审核",
            320: "质量管理部驳",
            321: "待质量负责人审核",
            322: "质量负责人驳回"
        };

        table.render({
            elem: '#appliesTableId',
            url: 'common/applies',

            cols: [[
                {type: "checkbox", width: 40},
                {field: 'applyId', width: 280, title: 'Apply ID', hide: true},
                {field: 'name', width: 480, title: '商品名称'},
                {field: 'wareId', width: 160, title: '商品编码'},
                {field: 'submitTime', width: 120, title: '提交日期'},
                {field: 'state', width: 120, title: '审核状态', templet: function (d) {
                        return  map[d["state"]] || '<span style="color: red">' + d["state"] + '</span>';
                    }},
                {field: 'approvetime', title: '审核时间', width: 180, templet : "<div>{{layui.util.toDateString(d.approvetime, 'yyyy-MM-dd HH:mm:ss')}}</div>"},
                {field: 'reject', width: 200, title: '是否驳回', templet: function (d) {
                        if ( !!d["reject"] ) {
                            if ( !!d['reset'] && 'Y'=== d['reset'] ) {
                                return '<span style="color: red">驳回<span style="color: darkcyan">(已标记为重新提交)</span></span>';
                            } else {
                                return '<span style="color: red">驳回</span>';
                            }
                        }
                        return "";
                    }
                },
                {field: 'opinion', width: 380, title: '提示'},
                {fixed: 'right', width:120, align:'center', toolbar: '#productAppliesTableBar'}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);

            //执行搜索重载
            table.reload('appliesTableId', {
                page: { curr: 1 }
                , where: data.field
            }, 'data');

            return false;
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {

            var data = obj.data;

            debugger;

            switch(obj.event){
                case 'reset':
                    if ( !!!data["reject"]  )  {
                        layer.msg('被驳回的商品才能重新上架！');
                        return;
                    }

                    layer.confirm('重新上架商品[' + data["name"] + ']?', {icon: 3, title:'提示'}, function(index){

                        $.ajax({
                            type: 'POST',
                            url: "/common/apply/reset",
                            cache: false,
                            data: {applyId: data["applyId"]},
                            success: function (result) {
                                var data1 = form.val("search-form");
                                if ( !!!result.code ) {
                                    table.reload('appliesTableId', { where: data1}, 'data');
                                    layer.msg('设置成功');
                                } else {
                                    layer.msg(result.msg || "设置失败");
                                }
                            },
                            dataType: "json"
                        });

                        layer.close(index);
                    });

                    break;
                case 'start':
                    layer.msg('启动上架');
                    break;
            }
        });

    });
</script>